package com.zxm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxm.result.EasyUiResult;
import com.zxm.result.GuhealthResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.zxm.mapper.SportDetailMapper;
import com.zxm.mapper.UserInfoMapper;
import com.zxm.pojo.SportDetail;
import com.zxm.pojo.SportDetailExample;
import com.zxm.pojo.SportDetailExample.Criterion;
import com.zxm.pojo.UserBodyInfoExample;
import com.zxm.pojo.UserBodyInfoExample.Criteria;
import com.zxm.pojo.UserInfo;
import com.zxm.pojo.UserInfoExample;
import com.zxm.service.SportDetailService;
import com.zxm.pojo.SportDetailExample;
/**
 * @author 张鑫铭
 */
@Service(interfaceName="com.zxm.service.SportDetailService")
public class SportDetailServiceImpl implements SportDetailService {
@Autowired
SportDetailMapper  sportDetailMapper;
@Autowired
UserInfoMapper userInfoMapper;
//用户查看运动数据中心
		/**
		 * 用户查询某个时间范围内的运动数据
		 * @param start  时间的开始
		 * @param end    时间的结束
		 * @param sportType  运动的类型（室内、室外、骑行） 传入参数视情况再做更改
		 * @return
		 */
	@Override
	public List<SportDetail> showSportDetailByCondition(Date start, Date end, String username, String sportType) {
		// TODO Auto-generated method stub
		SportDetailExample sde=new SportDetailExample();
		UserInfoExample uie=new UserInfoExample();
		UserInfoExample.Criteria c_userinfo=uie.createCriteria();
		c_userinfo.andUsernameEqualTo(username);
		List<UserInfo> userInfoList = userInfoMapper.selectByExample(uie);
		UserInfo userInfo =null;
		if(null!=userInfoList && userInfoList.size()>0){
			userInfo = userInfoList.get(0);
		}
		SportDetailExample.Criteria c=sde.createCriteria();
    	//当结束时间为空时，返回值为起始时间到今天的值
    	if(end==null) {
    		c.andStartTimeGreaterThan(start).andIdEqualTo(userInfo.getId()).andSportLocationTypeEqualTo(sportType);
    		List<SportDetail> list=sportDetailMapper.selectByExample(sde);
    		return list;
    	}
    	c.andStartTimeGreaterThanOrEqualTo(start).andEndTimeLessThanOrEqualTo(end).andIdEqualTo(userInfo.getId()).andSportLocationTypeEqualTo(sportType);
		List<SportDetail> list=sportDetailMapper.selectByExample(sde);
		return list;
	}

	@Override
	public GuhealthResult addSportDetailByConditon(String username, SportDetail sportDetail) {
		// 根据username查询出userId
		UserInfoExample uie=new UserInfoExample();
		uie.createCriteria().andUsernameEqualTo(username);
		List<UserInfo> userInfoList=userInfoMapper.selectByExample(uie);
		if(null == userInfoList || userInfoList.size()<=0){
			return GuhealthResult.fail();
		}
		UserInfo userInfo = userInfoList.get(0);
		sportDetail.setUserId(userInfo.getId());
		SportDetailExample example = new SportDetailExample();
		int i = sportDetailMapper.insertSelective(sportDetail);
		if(i>0){
			return GuhealthResult.success();
		}
		return GuhealthResult.fail();
	}
	@Override
	public SportDetail getNewSportDetail(String username) {
		return sportDetailMapper.selectByNewSportDetail(username);
	}

	//TODO 方法没完成
	//根据运动类型 查询出当前或者历史用户  分页展示
	@Override
	public EasyUiResult getSportUser(Integer pageNum, Integer pageSizes,String sportType, String status) {
		//分页
		PageHelper.startPage(pageNum, pageSizes);
		List<SportDetail> sportDetailList = sportDetailMapper.selectAllNewSportDetail(sportType);
		List<UserInfo> currentUsers = new ArrayList<>();
		List<UserInfo> historyUsers = new ArrayList<>();
		for (SportDetail sportDetail : sportDetailList) {
			UserInfo userInfo = userInfoMapper.selectByPrimaryKey(sportDetail.getUserId());
			if(null == sportDetail.getEndTime()){
				currentUsers.add(userInfo);
			}else{
				historyUsers.add(userInfo);
			}
		}
		PageInfo<UserInfo> pageInfo = null;
		if("current".equals(status)){
			//封装pageInfo对象
			pageInfo = new PageInfo<>(currentUsers);
		}else {
			pageInfo = new PageInfo<>(historyUsers);
		}
		//返回结果
		EasyUiResult result = new EasyUiResult();
		result.setRows(pageInfo.getList());
		result.setTotal(pageInfo.getTotal());
		return result;
	}

}
